package com.hzsun.rocketmq.spring.producer;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Author: DingJl
 * @Date: 2022/1/22 14:17
 * @Version 1.0
 */
@Component
public class RocketMQProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

//    @Value("${rocketmq.producer.send-message-timeout}")
    private Integer messageTimeOut=10000;

    private static String COLON = ":";
    private static String KEYS = "KEYS";

    /**
     * 发送普通消息
     */
    public SendResult sendMsg(String topic, String tags, String keys, String msgBody) throws UnsupportedEncodingException {
        MessageBuilder<String> msg = MessageBuilder.withPayload(msgBody);
        if (StringUtils.isNotBlank(keys)) {
            msg.setHeader(KEYS, keys);
        }

        String destination = null;
        if (StringUtils.isNotBlank(tags)) {
            destination = topic + COLON + tags;
        } else {
            destination = topic;
        }
        return rocketMQTemplate.syncSend(destination, msg.build());
    }

    /**
     * 发送有序消息
     */
    public SendResult sendQueueMsg(String topic, String tags, String keys,String hashkey, String msgBody) throws UnsupportedEncodingException {
        MessageBuilder<String> msg = MessageBuilder.withPayload(msgBody);
        if (StringUtils.isNotBlank(keys)) {
            msg.setHeader(KEYS, keys);
        }

        String destination = null;
        if (StringUtils.isNotBlank(tags)) {
            destination = topic + COLON + tags;
        } else {
            destination = topic;
        }
        return rocketMQTemplate.syncSendOrderly(destination, msg.build(),hashkey);
    }


    /**
     * 发送异步消息 在SendCallback中可处理相关成功失败时的逻辑
     */
    public void sendAsyncMsg(String topic, String tags, String keys, String msgBody, SendCallback sendCallback) throws UnsupportedEncodingException {
        MessageBuilder<String> msg = MessageBuilder.withPayload(msgBody);
        if (StringUtils.isNotBlank(keys)) {
            msg.setHeader(KEYS, keys);
        }

        String destination = null;
        if (StringUtils.isNotBlank(tags)) {
            destination = topic + COLON + tags;
        } else {
            destination = topic;
        }
        rocketMQTemplate.asyncSend(destination, msg.build(), sendCallback);
    }

    /**
     * 发送延时消息<br/>
     * 在start版本中 延时消息一共分为18个等级分别为：1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h<br/>
     */
    public void sendDelayMsg(String topic, String tags, String keys, String msgBody, Integer delayLevel) throws UnsupportedEncodingException {
        MessageBuilder<String> msg = MessageBuilder.withPayload(msgBody);
        if (StringUtils.isNotBlank(keys)) {
            msg.setHeader(KEYS, keys);
        }

        String destination = null;
        if (StringUtils.isNotBlank(tags)) {
            destination = topic + COLON + tags;
        } else {
            destination = topic;
        }
        rocketMQTemplate.syncSend(destination, msg.build(), messageTimeOut, delayLevel);
    }

    /**
     * 发送单向消息
     */
    public void sendOnewayMsg(String topic, String tags, String keys, String msgBody) throws UnsupportedEncodingException {
        MessageBuilder<String> msg = MessageBuilder.withPayload(msgBody);
        if (StringUtils.isNotBlank(keys)) {
            msg.setHeader(KEYS, keys);
        }

        String destination = null;
        if (StringUtils.isNotBlank(tags)) {
            destination = topic + COLON + tags;
        } else {
            destination = topic;
        }
        rocketMQTemplate.sendOneWay(destination, msg.build());
    }

    /**
     * 发送事务消息
     *  监听类实现 @TransactionListenerImpl
     */
    public void sendTranstionMsg(String topic, String tags, String keys, String msgBody) throws UnsupportedEncodingException {
        MessageBuilder<String> msg = MessageBuilder.withPayload(msgBody);
        if (StringUtils.isNotBlank(keys)) {
            msg.setHeader(KEYS, keys);
        }

        String destination = null;
        if (StringUtils.isNotBlank(tags)) {
            destination = topic + COLON + tags;
        } else {
            destination = topic;
        }

       rocketMQTemplate.sendMessageInTransaction(destination,msg.build(),null);
    }


}
